
#include "userconnectionmanager.h"

UserConnectionManager::UserConnectionManager(boost::asio::io_service& io)
  : 
  LoggingService("CONN-MGR")
{
  m_io_service = &io;
}


/*
UserConnection * UserConnectionManager::newUserConnection()
{
  UserConnection * c = new UserConnection(m_io_service, this);

//  m_conn_unauthorized.push_back(c);
  return c;
}
*/


void UserConnectionManager::closeAll()
{

  for (unsigned int i = 0; i < m_conn_unauthorized.size(); i++)
  {
    boost::shared_ptr<tcp::socket> s;

    s = m_conn_unauthorized[i]->getUserSocketFrom();
    if (s.get() != 0)
      s.get()->close();

    
    s = m_conn_unauthorized[i]->getUserSocketTo();
    if (s.get() != 0)
      s.get()->close();
  }

}


UserConnection * UserConnectionManager::registerUserSocket(const boost::shared_ptr<tcp::socket>& socket, bool direction)
{
  std::string address = socket->remote_endpoint().address().to_string();

  UserConnection * conn = getUserConnectionByAddress(address);

  if (conn == 0)
  {
    // create new connection
    debug() << "address " << address << " unknown. creating a new user connection" << endl;

    conn = new UserConnection(m_io_service, this, address);


    m_conn_unauthorized.push_back(conn);

    m_address_to_connection.insert(std::pair<std::string, UserConnection*>(address, conn));
  }
  else
  {
    // update the existing connection
    debug() << "address " << address << " known, userconnection: " << conn->toString() << ". updating" << endl;

  }

  if (direction)
    conn->updateUserSocketFrom(socket);
  else 
    conn->updateUserSocketTo(socket);

  printConnections();

  return conn;
}



/*
UserConnection * UserConnectionManager::registerUserSocketFrom(const boost::shared_ptr<tcp::socket>& socket)
{
  std::string address = socket->remote_endpoint().address().to_string();

  UserConnection * conn = getUserConnectionByAddress(address);

  if (conn == 0)
  {
    // create new connection
    debug() << "address " << address << " unknown. creating a new user connection" << endl;

    conn = new UserConnection(m_io_service, this, address);

    conn->updateUserSocketFrom(socket);

    m_conn_unauthorized.push_back(conn);

    m_address_to_connection.insert(std::pair<std::string, UserConnection*>(address, conn));
  }
  else
  {
    // update the existing connection
    debug() << "address " << address << " known, userconnection: " << conn->toString() << ". updating" << endl;

    conn->updateUserSocketFrom(socket);
  }
  printConnections();
  return conn;
}
*/

UserConnection * UserConnectionManager::getUserConnectionByAddress(const std::string& address)
{
  std::map<std::string, UserConnection*>::iterator it = m_address_to_connection.find(address);

  if (it != m_address_to_connection.end())
  {
    return (*it).second;
  }
  return 0;
}

void UserConnectionManager::printConnections()
{
  debug() << "*** Active Connections ***" << endl;
  for (unsigned int i = 0; i < m_conn_unauthorized.size(); i++)
  {
    debug() << "  " << i << " " << m_conn_unauthorized[i]->toString() << endl;
  }
}
